<?php 
require_once 'init.php';
require_once 'ckLogin.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
//定义表名
define("TABLENAME","tuijian");
define("TABLENAME1","users");
//define("TABLENAME2","category");
//提交处理
if($_GET['act']=='del'){
	$db->sql="delete from `tuijian` where id = {$_GET['id']}";
	$db->doExecute();
}
$isupdate = 0;
if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
	$id=$_POST['tuijianID'];
	$url = $_POST['u_url'];
	$title = $_POST['u_title'];
	$prePrice = $_POST['u_prePrice'];
	$nowPrice = $_POST['u_nowPrice'];
	$pic = $_POST['u_pic'];
	$tuijian = htmlspecialchars($_POST['u_tuijian']);
	$isuse = $_POST['u_isuse'];
	$pubdate = $_POST['u_pubdate']?$_POST['u_pubdate']:"0000-00-00";
	$dianping = htmlspecialchars($_POST['u_dianping']);
	$commission = $_POST['u_commission']?$_POST['u_commission']:0;
	//更改
	$db->sql="UPDATE ".TABLENAME." SET `url` = '{$url}' , title = '{$title}' , prePrice = '{$prePrice}' , nowPrice = '{$nowPrice}' , commission = '{$commission}' , pic = '{$pic}' , tuijian = '{$tuijian}' , isuse = '{$isuse}' , pubdate = '{$pubdate}' , dianping = '{$dianping}' , updater = '{$_SESSION["userName"]}' WHERE id = {$id}";
	$isupdate = $db->doExecute();
	//是否是第一次通过
	if($isuse==1 || $isuse==-1){
	    $db->sql="SELECT p.isfirst,p.openid FROM ".TABLENAME." p WHERE p.id = {$id}";
	    $tuijianInfo  = $db->doResult();
	    $isfirst = $tuijianInfo['isfirst'];
	    $openid  = $tuijianInfo['openid'];
	    if ($isfirst==1) {
	        //修改
	    	$db->sql="UPDATE ".TABLENAME." SET `isfirst` = 0  WHERE id = {$id}";
	        $db->doExecute();
	        if ($isuse==1) {
	        	$sendmessage = "亲~您推荐的·{$title}·已被录用，已给您增加200金币。";
	        	//加金币
	        	$db->sql="UPDATE ".TABLENAME1." SET `GoldCoinNumber` = `GoldCoinNumber`+200  WHERE Identifier = '{$openid}'";
	        	$isAddGold =$db->doExecute();
	        }
	        if ($isuse==-1) {
	            $sendmessage = "亲~您的推荐没有得到通过T-T，请再接再励哦！";
	        }
	        //发送消息
	        $db->sql="INSERT INTO `message`(`date`,`message`,`pid`,`openid`,`noread`) VALUES('".date("Y-m-d")."','{$sendmessage}',-1,'{$openid}',1)";
	        $isAddMes  = $db->doExecute();
	    }
	}
	//操作记录
	$db->sql = "INSERT INTO goodschange (changeid,`table`,goodsid,oldsaletime,operate,updater,changetime) VALUES({$id},'".TABLENAME."',1,'{$pubdate}','修改','{$_SESSION["userName"]}',".time().")";
	$db->doExecute();
				
	
}
//条件
$where = "WHERE 1=1 ";
if(!empty($_GET['startTime']) || !empty($_GET['endTime']) || !empty($_GET['goodsid'])){
	if(!empty($_GET['startTime'])) $where.= "AND p.publishDate>='".$_GET['startTime']."' ";
	if(!empty($_GET['endTime'])) $where.= "AND p.publishDate<='".$_GET['endTime']."' ";
	if(!empty($_GET['goodsid'])) $where.= "AND p.goodsID='".$_GET['goodsid']."' ";
}
//排序publishDate Updater shared collected
if(!empty($_GET['sort'])){
	$sortarray = explode('_', $_GET['sort']);
	$order = "p.".$sortarray[0]." ".$sortarray[1].",";
}else{
	$order = '';
}
//总条数
$db->sql="SELECT count(p.id) count_id FROM ".TABLENAME." p ".$where;
$count = $db->doResultOne();
//分页
if(empty($_GET['page'])){$page = 1;}else{$page=$_GET['page'];}
if(empty($_GET['pagesize'])){$pagesize = PAGESIZE;}
else{$pagesize = $_GET['pagesize'];}
$pageInfo = page($count, $pagesize, $page);
$maxPage = $pageInfo['maxPage'];
$start = $pageInfo['start'];
$pages = $pageInfo['pages'];

$db->sql="SELECT p.* FROM ".TABLENAME." p ".$where." ORDER BY ".$order." p.id DESC LIMIT ".$start.",".$pagesize;
$pictures = $db->doResultList2();
//var_dump($pictures);die();
//url
$url= "?startTime=".$_GET['startTime']."&endTime=".$_GET['endTime']."&pagesize=".$_GET['pagesize']."&goodsid=".$_GET['goodsid']."&sort=".$_GET['sort'];
$url2="?startTime=".$_GET['startTime']."&endTime=".$_GET['endTime']."&pagesize=".$_GET['pagesize']."&goodsid=".$_GET['goodsid'];
$url3="?startTime=".$_GET['startTime']."&endTime=".$_GET['endTime']."&pagesize=".$_GET['pagesize']."&goodsid=".$_GET['goodsid'];
$url4="?updater=".$_GET['updater']."&pagesize=".$_GET['pagesize']."&goodsid=".$_GET['goodsid'];

//分页
function page($total,$size,$page){
	//最大页数
	if($total%$size==0){
		$maxPage = $total/$size;
	}else{
		$maxPage = (int)($total/$size)+1;
	}
	//开始条数
	$start = ($page-1)*$size;
	//分页
	if($maxPage>=5){
		if($page<=2){
			$pages=array(1,2,3,4,5);
		}elseif($page>=($maxPage-1)){
			$pages=array($maxPage-4,$maxPage-3,$maxPage-2,$maxPage-1,$maxPage);
		}else{
			$pages=array($page-2,$page-1,$page,$page+1,$page+2);
		}
	}else{
		for($i=1;$i<=$maxPage;$i++){
			$pages[]=$i;
		}
	}
	if($page<=1){$previous=1;}else{$previous=$page-1;} 
	if($page>=$maxPage) {$next = $maxPage;}else{ $next = $page+1;}
	return array('maxPage'=>$maxPage,'start'=>$start,'pages'=>$pages,'previous'=>$previous,'next'=>$next);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simpla Admin</title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css"
	media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css"
	media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css"
	media="screen" />
<!-- Colour Schemes
		Default colour scheme is green. Uncomment prefered stylesheet to use it.
		<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />
		<link rel="stylesheet" href="resources/css/red.css" type="text/css" media="screen" />  
		-->
<!-- Internet Explorer Fixes Stylesheet -->
<!--[if lte IE 7]>
			<link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen" />
		<![endif]-->
<!--                       Javascripts                       -->
<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>

<script type="text/javascript">
$(function($) {
	$(".editbutton").click(function(){
		var id = $(this).data("id");
		var isuse = $(this).data("isuse");
		var tr = $(this).parents("tr");
		$("#u_url").val(tr.children("td").eq(2).text());
		$("#u_title").val(tr.children("td").eq(3).text());
		$("#u_prePrice").val(tr.children("td").eq(4).text());
		$("#u_nowPrice").val(tr.children("td").eq(5).text());
		$("#u_commission").val(tr.children("td").eq(6).text());
		$("#u_pic").val(tr.children("td").eq(9).find("img").attr("src"));
		$("#u_tuijian").html(tr.children("td").eq(7).text());
		$("#u_dianping").html(tr.children("td").eq(11).text());
		if(isuse==0) $("#u_isuse_0").attr("checked",true);
		if(isuse==1) $("#u_isuse_1").attr("checked",true);
		if(isuse==-1) $("#u_isuse_-1").attr("checked",true);
		if(tr.children("td").eq(10).text()!='0000-00-00' && tr.children("td").eq(10).text()) $("#u_pubdate").val(tr.children("td").eq(10).text());
		$("#tuijianID").val(id);
		$("#message_click").click();
	});
	$(".formsubm").live("click", function () {
		//alert($("input[name='u_isuse']:checked").val());
	    //return false;
	} );
	//删除商品
	$(".delbutton").click(function(){
		var id = $(this).data("id");
		//alert(id);return false;
		if(!confirm("是否删除该商品！")){
			return false;
		}
	});
	//商品上下架
	$(".isSalebutton").click(function(){
		var _this = $(this);
		var id = _this.data("id");
		var status = _this.data("status");
		if(status==1) {var word = "是否下架该商品？";}
		if(status==2) {var word = "是否上架该商品？";}
		//alert(id);return false;
		if(confirm(word)){
			$.ajax({
				type: "GET",
				url: "youpinAjax.php",
				data: "id="+id+"&act=isSale&table=content&",
				dataType: "html",
				success: function(rs){
					if(rs==1) {_this.text("上架中");alert("上架成功！！");};
					if(rs==2) {_this.text("下架中");alert("下架成功！！");};
					_this.data("status",rs);
				}
			});
		}
	});
	//多数据操作
	$(".xiugai").click(function(){
		//alert($("#caozuo option:selected").val());
			var text="";  
	        $("input[name=picSel]").each(function() {  
	            if ($(this).attr("checked")) {  
	                text += ","+$(this).val();  
	            }  
	        });
	        //alert(text);
	        window.location.replace('moreEdit.php?ids='+text);
		
	});
	//导出
	$("#download").click(function(){
		var startTime = $('#startTime').val();
		var endTime   = $('#endTime').val();
		var updater = $("#updater").val();
		//var pagesize = $("#pagesize").val();
		var cateid = $("#cateid").val();
		
		if(startTime && endTime){
			if(startTime > endTime){
				alert('开始时间大于结束时间');
				return false;
			}else{
				window.open('downloadExcel2.php?startTime='+startTime+'&endTime='+endTime+'&updater='+updater+'&cateid='+cateid);
			}
		}else{
			alert("时间不能为空");
			return false;
		}
	});
	//批量删除
	$(".piliangshanchu").click(function(){
		if(confirm("是否删除这些商品！")){
			var ids='';
			$("input[name=picSel]:checked").each(function() {
				ids+=$(this).val()+',';
				$(this).parents("tr").remove();
			});
			$.ajax({
				type: "GET",
				url: "youpinAjax.php",
				data: "ids="+ids+"&act=piliangshanchu",
				dataType: "html",
				success: function(date){
					//alert(date);
					
				}
			});
			//alert(ids);
		}
	});
});

</script>

<!-- jQuery Configuration -->
<script type="text/javascript" src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
<!-- jQuery my97datepicker -->
<script type="text/javascript" src="resources/scripts/My97DatePicker/WdatePicker.js"></script>
<!-- jQuery colorbox -->
<link rel="stylesheet" href="resources/css/colorbox.css" />
<script src="resources/scripts/colorbox/jquery.colorbox.js"></script>
<script type="text/javascript">
$(function($) {
	$(".group-pic").colorbox({rel:'group-pic',width:"50%", height:"75%"});
});
</script>
</head>
<body>
	<div id="body-wrapper">
	<?php if($isupdate>0):?>
	    <div class="notification success png_bg">
			<a href="#" class="close"><img src="resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close"></a>
			<div>
				修改成功<?php if($isAddGold) echo ",并给用户添加了200金币";if($isAddMes) echo "并给用户发送了消息：'{$sendmessage}'";?>
			</div>
		</div>
	<?php endif;?>
		<!-- Wrapper for the radial gradient background -->
		<a href="#messages" rel="modal" id="message_click"></a>
		<div id="messages" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p style="">
				<strong>简短描述输入及修改</strong>
			</p>
			<form action="<?php echo $url."&page=".$page;?>" method="post">
				<fieldset>
					地址：
					<input class="text-input " type="text" id="u_url" name="u_url" style="width: 310px;"/><br />
					标题：
					<input class="text-input " type="text" id="u_title" name="u_title" style="width: 310px;"/><br />
					原价：
					<input class="text-input " type="text" id="u_prePrice" name="u_prePrice" style="width: 50px;"/>
					现价：
					<input class="text-input " type="text" id="u_nowPrice" name="u_nowPrice" style="width: 50px;"/>
					佣金：
					<input class="text-input " type="text" id="u_commission" name="u_commission" style="width: 50px;"/>%<br />
					推荐理由：
					<textarea class="textarea" id="u_tuijian" name="u_tuijian" cols="79" rows="5"></textarea><br />
					图片地址：
					<input class="text-input " type="text" id="u_pic" name="u_pic" style="width: 295px;"/><br />
					点评：
					<textarea class="textarea" id="u_dianping" name="u_dianping" cols="79" rows="5"></textarea><br />
					<input type="radio" id="u_isuse_0" name="u_isuse" value="0"/>未审核：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<input type="radio" id="u_isuse_1" name="u_isuse" value="1"/>通过：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<input type="radio" id="u_isuse_-1" name="u_isuse" value="-1"/>不通过：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
					发布日期：
					<input class="text-input " type="text" id="u_pubdate" name="u_pubdate" onClick="WdatePicker()" value="<?php echo date("Y-m-d");?>"/><br />
					
					<input id="tuijianID" name="tuijianID" type="hidden" value="" />
					<span style="color:red;">第一次通过将给用户添加200金币。并发送消息。</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<input class="button formsubm" type="submit" value="修改" /> 
					<a href="javascript:void(0);" onclick="$(this).parents('#facebox').hide().siblings('#facebox_overlay').remove();">关闭</a>
				</fieldset>
			</form>
		</div>
		<!-- Wrapper for the radial gradient background -->
		<div id="messages1" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p style="">
				<strong>简短描述输入及修改</strong>
			</p>
			<form action="<?php echo $url."&page=".$page;?>" method="post">
				<fieldset>
				    <label for="sendmessage1">
    				    <input type="radio" value="1" id="sendmessage1" name="sendmessage"/>
    					亲~您的推荐没有得到通过T-T，请再接再励哦！
					</label>
					<label for="sendmessage2">
    				    <input type="radio" value="1" id="sendmessage2" name="sendmessage"/>
    					<input class="text-input " type="text" id="sendmessageself" name="sendmessageself" style="width: 295px;"/>
					</label>
					<input class="button formsubm" type="submit" value="发送" /> 
					<a href="javascript:void(0);" onclick="$(this).parents('#facebox').hide().siblings('#facebox_overlay').remove();">关闭</a>
				</fieldset>
			</form>
		</div>
		<!-- End #messages -->
		<div id="main-content">
			<!-- Main Content Section with everything -->
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro" ></p>
			<div class="clear"></div>
			<!-- End .clear -->
			<div class="content-box">
				<!-- Start Content Box -->
				<div class="content-box-header">
					<h3><?php echo WHAT;?>列表</h3>
					<!-- <ul class="content-box-tabs">
						<li><a href="#tab1" class="default-tab">Table</a></li>
						href must be unique and match the id of target div
						<li><a href="#tab2">Forms</a></li>
					</ul> -->
					<div class="clear"></div>
				</div>
				<!-- End .content-box-header -->
				<div class="content-box-content">
					
					<form method="get" action="">
						商品ID：<input type="text" class="text-input" id="goodsid" name="goodsid" value="<?php echo $_GET['goodsid'];?>" size="12"/> 
						时间：
						<input type="text" class="text-input" id="startTime" name="startTime" onClick="WdatePicker()" value="<?php echo $_GET['startTime'];?>" size="10"/> 
						-
						<input type="text" class="text-input" id="endTime" name="endTime" onClick="WdatePicker()" value="<?php echo $_GET['endTime'];?>" size="10"/>
						&nbsp;&nbsp;&nbsp;
						每页<input class="text-input" id="pagesize" name="pagesize" type="text" size="2" value="<?php echo $pagesize;?>"/>条
						&nbsp;&nbsp;&nbsp;
						<button class="button" id="zhixing" type="submit">搜索</button>
						<button class="button" id="download" type="submit">导出</button>						
						<input type="hidden" id="cateid" name="cateid" value="<?php echo $_GET['cateid'];?>"/>
					</form>
					<hr />
					<div class="clear"></div>
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->

						<table>
							<thead>
								<tr>
									<td colspan="17">
										<div class="bulk-actions align-left">
											<button class="button piliangshanchu" >批量删除</button>
										</div>
										<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="<?php echo $url;?>&page=1" title="First Page">&laquo; First</a>
											<a href="<?php echo $url."&page=".$pageInfo['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="<?php echo $url."&page=".$item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo $url."&page=".$pageInfo['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo $url."&page=".$maxPage;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<hr class="clear" />
									</td>
								</tr>
							</thead>
							<thead>
								<tr>
									<th><input class="check-all" type="checkbox" value="-1"/></th>
									<th>编号</th>
									<th style="width: 10%;" class="taobao_url_css">地址</th>
									<th style="width: 6%;">标题</th>
									<th style="width: 6%;">原价</th>
									<th style="width: 6%;">现价</th>
									<th style="width: 6%;">佣金</th>
									<th style="width: 10%;">推荐<br />理由</th>
									<th>操作</th>
									<th>图片</th>
									<th>日期</th>
									<th style="width: 10%;">点评</th>
									<th>分享</th>
									<th>收藏</th>
								</tr>
							</thead>
							<tbody>
							<?php if(count($pictures)>0):?>
							<?php foreach ($pictures as $key=>$item):?>
								<tr>
									<td><input type="checkbox" name="picSel" value="<?php echo $item['id'];?>"/></td>
									<td><?php echo $item['id'];?></td>
									<td><a href="<?php echo $item['url'];?>" target="_blank"><?php echo $item['url'];?></a></td>
									<td><a href="<?php echo $item['url'];?>" target="_blank"><?php echo $item['title'];?></a></td>
									<td><?php echo $item['prePrice'];?></td>
									<td><?php echo $item['nowPrice'];?></td>
									<td><?php echo $item['commission'];?></td>
									<td><?php echo $item['tuijian'];?></td>
									<td>
										<!-- Icons --> 
										
										<a class="editbutton" data-id="<?php echo $item['id'];?>" data-isuse="<?php echo $item['isuse'];?>" style="cursor: pointer;<?php if($item['isuse']==1) echo 'color: black;';if($item['isuse']==-1) echo 'color: gray;';?>"><?php if($item['isuse']==1) echo '已通过';if($item['isuse']==0) echo '审核';if($item['isuse']==-1) echo '不通过';?></a><br />
										<a href="<?php echo $url."&page=".$page."&act=del&id=".$item['id'];?>" class="delbutton" style="cursor: pointer;">删除</a><br /><br />
										<!-- <a href="#" title="Edit Meta"><img src="resources/images/icons/hammer_screwdriver.png" alt="Edit Meta" /></a> -->
									</td>
									<td>
										<p><a class="group-pic" href="<?php echo $item['pic'];?>">
											<img src="<?php echo $item['pic']; ?>" onerror="this.src='<?php echo $item['pic'];?>'" alt="" width="50px;" height="50px;"/>
										</a></p>
									</td>
									<td><?php echo $item['pubdate'];?></td>
									<td><?php echo $item['dianping'];?></td>
									<td><?php echo $item['shared'];?></td>
									<td><?php echo $item['collected'];?></td>
								</tr>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="9" style="text-align: center;">没有<?php echo WHAT;?></td></tr>
							<?php endif;?>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="17">
										<div class="bulk-actions align-left">
											<button class="button piliangshanchu" >批量删除</button>
										</div>
										<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="<?php echo $url;?>&page=1" title="First Page">&laquo; First</a>
											<a href="<?php echo $url."&page=".$pageInfo['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="<?php echo $url."&page=".$item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo $url."&page=".$pageInfo['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo $url."&page=".$maxPage;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<div class="clear"></div>
									</td>
								</tr>
							</tfoot>

						</table>

					</div>
					<!-- End #tab1 -->


				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
